<template>
  <div v-if="showUpdateSoftware">
    <div class="updateSoftware" v-if="!isDw">
      <div class="update-info">
        <div class="option">提示</div>
        <div class="sub-option">有新的版本可供更新！</div>
        <div class="tip-wrap">
          <div class="tip">软件名称:{{ updateInfo.fileName }}</div>
        </div>
        <div class="button-wrap">
          <el-button type="primary" @click.stop="download"
            >下载并更新</el-button
          >
        </div>
      </div>
    </div>
    <div class="download-wrap" v-if="isDw">
      <div class="download-info">
        <el-progress
          type="circle"
          :percentage="progressBarVal"
          :status="status"
        ></el-progress>
        <div class="tip">下载中...,完成后将自动打开。</div>
      </div>
    </div>
  </div>
</template>
<script>
import { ipc } from "@/utils/ipcRenderer";
import { ipcApiRoute, specialIpcRoute } from "@/api/main";

export default {
  name: "updateSoftware",
  data() {
    return {
      updateInfo: {},
      status: "",
      isDw: false,
      progressBarVal: 0,
    };
  },
  computed: {
    showUpdateSoftware() {
      return this.$store.state.app.showUpdateSoftware;
    },
  },
  watch: {
    showUpdateSoftware(val) {
      if (val) {
        this.init();
      }
    },
  },
  methods: {
    getSoftware() {
      ipc
        .invoke(ipcApiRoute.checkForUpdater)
        .then((res) => {
          console.log("res", res);
          if (
            res &&
            res.hasOwnProperty("updateInfo") &&
            res.updateInfo.status == "1"
          ) {
            this.$store.dispatch("app/toggleUpdateSoftware", true);
            this.updateInfo = res.updateInfo;
            if (res.updateInfo && res.updateInfo.path) {
              if (
                Array.isArray(res.updateInfo.files) &&
                res.updateInfo.files.length
              ) {
                this.updateInfo.fileName =
                  res.updateInfo.files[res.updateInfo.files.length - 1].url;
              }
            }
          }
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
    getOperatingSystem() {
      const platform = navigator.platform.toLowerCase();
      if (platform.includes("mac")) return "Mac";
      if (platform.includes("win")) return "Windows";
      return "Unknown";
    },
    download() {
      this.isDw = true;
      let a = document.createElement("a");
      let url = "";
      url = `https://images.huoxingy.com/x-aiwd-software/${this.updateInfo.fileName}`;
      //   if (this.getOperatingSystem() == "Mac") {
      //     url  = `https://images.huoxingy.com/x-aiwd-software/${this.updateInfo.name}`";
      //   }
      a.href = url;
      a.download = `${this.updateInfo.fileName}`;
      a.click();
      window.URL.revokeObjectURL(url);
    },
    init() {
      try {
        ipc.invoke(ipcApiRoute.updateSoftware, {});
      } catch (error) {
        console.log("error", error);
      }
      try {
        ipc.on("updateSoftwareProgress", (event, result) => {
          console.log("result", result);
          if (result) {
            let tempResult = JSON.parse(result);
            if (tempResult.hasOwnProperty("progressBarVal")) {
              this.progressBarVal = tempResult.progressBarVal;
              if (Number(tempResult.progressBarVal) == 100) {
                this.status = "success";
              }
            }
          }
        });
      } catch (error) {}
      try {
        this.getSoftware();
      } catch (error) {
        console.log("error", error);
      }
    },
  },
  mounted() {
    //
    this.init();
    //
  },
};
</script>
<style lang="scss" scoped>
.updateSoftware,
.download-wrap {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 99999;
  background-color: rgba(0, 0, 0, 0.79);
  .update-info {
    width: 350px;
    border-radius: 19px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .option {
      font-size: 16px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0;
    }
    .sub-option {
      text-align: center;
      font-size: 14px;
      color: #333;
    }
    .tip-wrap {
      padding: 20px 0;
      text-align: center;
      color: green;
      font-size: 14px;
      font-weight: bold;
    }
    .button-wrap {
      padding: 20px 0;
      display: flex;
      justify-content: center;
      align-items: center;
      button {
        display: inline-block;
        width: 200px;
        height: 35px;
        border: none;
        outline: none;
      }
    }
  }
  .download-info {
    width: 250px;
    height: 250px;
    border-radius: 35px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    .tip {
      width: 100%;
      text-align: center;
      font-size: 12px;
      color: #564dca;
    }
  }
}
</style>